<html>
<style type="text/css">
<!--
.style3 {
	font-size: 18px;
	font-weight: bold;
	color: #993300;
}
.style7 {
	color: #000000;
	font-weight: bold;
}
.style8 {color: #990033}
-->
</style>
<body>
<table width="466" height="319">
  <tr>
    <td height="38" bgcolor="#FFFFCC"><h1 class="style8"><u>Calculator</u></h1></td>
  </tr>
  <tr>
    <td height="41" bgcolor="#D9ECFF">
      <strong>Enter your regular expression below and press calculate.<br>
Thank you for using our Calculator.</strong></td>
  </tr>
  <tr>
    <td height="40" align="left" bgcolor="#CCFFCC"><br>
      <form action="calculator.php" method="POST"><input type="text" name="exp"><input type="submit" value="Calculate">
      </form></td>
  </tr>
  <tr>
    <td height="22" bgcolor="#FFCCCC"><h3><span class="style3"><u>Our Calculator Rules</u></span></h3>
    </td>
  </tr>
  <tr>
    <td height="59" bgcolor="#FFFFCC"><span class="style7">1. Only numbers and +, -, *, and / operators are allowed.<br>
2. The evaluation follows the standard operator precedence.<br>
3. Our calculator does not support parentheses.
    </span></td>
  </tr>
  <tr>
    <td height="57" bgcolor="#99CCFF">
<?php

$pattern1 = '#[ ]*[-]?\d*[.]?\d+([ ]*([+]{1}|[-]{1}|[*]{1}|[/]{1}|)[ ]*[-]?\d*[.]?\d+)*[ ]*#';
$pattern2 = '#[/]{1}[ ]*[-]?[ ]*[0]{1}#';
$pattern3 = '#[-]{1}[-]{1}#';

$s1 = $_POST["exp"];
preg_match($pattern1,$s1,$matches);

if (preg_match($pattern2,$s1)>0)
{
	echo ("Division by ZERO is not allowed!");
}

elseif (preg_match($pattern3,$s1)>0)
{
	echo ("Enter 'space' between subtraction and negation!<br>");
	echo ("e.g. a- -b");
}

elseif ($matches[0]==$s1)
{
	if ($s1!="")
	{
		eval("\$ans= $s1;");
		echo ("ANSWER = ".$ans);
	}
}

else
{
	echo ("Incorrect regular expression!<br/>");
	echo ("PLEASE follow our calculator rules above.");
}

?>
</td>
  </tr>
  <tr>
    <td height="28"><em>Created by Vahe Musinyan and Arman Abrahamyan</em></td>
  </tr>
</table>
<br>